package com.ruoyi.system.repository.service.impl;

import java.util.List;

import com.ruoyi.system.repository.domain.Member;
import com.ruoyi.system.repository.mapper.MemberMapper;
import com.ruoyi.system.repository.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 会员Service业务层处理
 *
 * @author qiang
 * @date 2021-01-01
 */
@Service
public class MemberServiceImpl implements IMemberService {
    @Resource
    private MemberMapper memberMapper;

    /**
     * 查询会员
     *
     * @param phone 会员ID
     * @return 会员
     */
    @Override
    public Member selectMemberById(String phone) {
        return memberMapper.selectMemberById(phone);
    }

    /**
     * 查询会员列表
     *
     * @param member 会员
     * @return 会员
     */
    @Override
    public List<Member> selectMemberList(Member member) {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增会员
     *
     * @param member 会员
     * @return 结果
     */
    @Override
    public int insertMember(Member member) {
                                                                                                                                        return memberMapper.insertMember(member);
    }

    /**
     * 修改会员
     *
     * @param member 会员
     * @return 结果
     */
    @Override
    public int updateMember(Member member) {
                                                                                                                                        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除会员
     *
     * @param phones 需要删除的会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(String[] phones) {
        return memberMapper.deleteMemberByIds(phones);
    }

    /**
     * 删除会员信息
     *
     * @param phone 会员ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(String phone) {
        return memberMapper.deleteMemberById(phone);
    }

    @Override
    public String login(Member user){
        if(user.getPhone().equals("")){
            System.out.println("手机号是空的，无法登录");
            return "";
        }
        Member successUser=memberMapper.login(user);
        if(null==successUser){
            return "";
        }
        return successUser.getPhone();
    }

    @Override
    public Member findByPhone(String phone){
        if(phone.equals("")){
            System.out.println("手机号是空的，无法查找");
            Member user=new Member();
            return user;
        }
        Member successUser=memberMapper.findByPhone(phone);
        return successUser;
    }
    @Override
    public String findByPhoneAndPwd(Member user){
        System.out.println(user.getPhone());
        if(user.getPhone().equals("")){
            return "未登录";
        }
        Member successUser=memberMapper.login(user);
        if(null!=successUser){
            return "";
        }
        return "原始密码输入错误，请检查";
    }
    @Override
    public String changeUser(Member user){
        //调用mapper中的方法
        Member beforeUser=memberMapper.findByPhone(user.getPhone());
        if(beforeUser==null){
            return "您尚未登录，请先登录";
        }
        if(beforeUser.getGender().equals(user.getGender())&&beforeUser.getBirthday().equals(user.getBirthday())
                &&beforeUser.getUsername().equals(user.getUsername())){
            return "修改信息无变化";
        }else{
            int count=memberMapper.changeUser(user);
            if(count==0){
                return "修改失败，请稍后修改";
            }
            return "修改成功";
        }
    }
    @Override
    public String logon(Member user){
        if(user.getPhone().length()!=11){
            return "此手机号有误，注册失败";
        }
        if(memberMapper.findByPhone(user.getPhone())!=null){
            return "此手机号已注册，注册失败";
        }
        user.setUsername("默认");
        user.setLevel((long)5);
        user.setGender("女");
        user.setBirthday("2020/09/05");
        int number=memberMapper.logon(user);
        if(number>0){
            return "";
        }
        return "注册失败";
    }
    @Override
    public String changePwd(Member user){
        int count=memberMapper.changePwd(user);
        if(count!=0){
            return "";
        }
        return "修改失败，请稍后重试";
    }

    @Override
    public String judge(String openid){
        return memberMapper.judge(openid);
    }

    @Override
    public String addOpenid(String openid,String phone){
        int count=memberMapper.addOpenid(openid,phone);
        if(count!=0){
            return "";
        }
        return "注册失败，请稍后重试";
    }

}
